package com.buaa.divinationmaster.core.algorithm;

import com.buaa.divinationmaster.core.constants.DivinationType;

/**
 * 占卜结果基类
 * 所有具体占卜结果类都应继承此类
 */
public abstract class DivinationResult {
    
    protected DivinationType type;
    protected String question;
    protected long timestamp;
    protected boolean isSuccessful;
    protected String errorMessage;
    
    // 基础构造函数
    public DivinationResult(DivinationType type) {
        this.type = type;
        this.timestamp = System.currentTimeMillis();
        this.isSuccessful = true;
    }
    
    // 带问题的构造函数
    public DivinationResult(DivinationType type, String question) {
        this(type);
        this.question = question;
    }
    
    // Getter和Setter方法
    public DivinationType getType() { return type; }
    public void setType(DivinationType type) { this.type = type; }
    
    public String getQuestion() { return question; }
    public void setQuestion(String question) { this.question = question; }
    
    public long getTimestamp() { return timestamp; }
    public void setTimestamp(long timestamp) { this.timestamp = timestamp; }
    
    public boolean isSuccessful() { return isSuccessful; }
    public void setSuccessful(boolean successful) { isSuccessful = successful; }
    
    public String getErrorMessage() { return errorMessage; }
    public void setErrorMessage(String errorMessage) { 
        this.errorMessage = errorMessage;
        this.isSuccessful = (errorMessage == null || errorMessage.isEmpty());
    }
    
    /**
     * 获取占卜结果的主要内容（需要子类实现）
     * @return 结果内容
     */
    public abstract String getMainContent();
    
    /**
     * 获取占卜结果的JSON表示（用于存储）
     * @return JSON字符串
     */
    public abstract String toJsonString();
    
    /**
     * 从JSON字符串恢复占卜结果（需要子类实现）
     * @param json JSON字符串
     */
    public abstract void fromJsonString(String json);
    
    /**
     * 获取结果的简短摘要
     * @return 摘要字符串
     */
    public abstract String getSummary();
    
    /**
     * 获取格式化的时间字符串
     * @return 格式化时间
     */
    public String getFormattedTime() {
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new java.util.Date(timestamp));
    }
    
    @Override
    public String toString() {
        return "DivinationResult{" +
                "type=" + type +
                ", question='" + question + '\'' +
                ", timestamp=" + timestamp +
                ", isSuccessful=" + isSuccessful +
                '}';
    }
}
